package com.twang.stock.design.impl;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.design.IndexDataTask;
import com.twang.stock.dto.IndexedInfo;
import com.twang.stock.entity.ex.TodayQinXuDto;
import com.twang.stock.mapper.TodayQinXuMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.task.QueryBanKuaiAndDaPan;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

/**
 * 今日大盘
 */

@Service
@Slf4j
public class TodayQinXuTask extends IndexedInfo implements IndexDataTask {
    public static String cache_key = "TodayQinXuTask";
    public static String cache_key_minutes = "TodayQinXuTask_minutes";
    public static String cache_key_toDay = "TodayQinXuTask_today";
    public static String cache_key_yesToday = "TodayQinXuTask_yesToday";





    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private TodayQinXuMapper todayQinXuMapper;

    @Override
    public void execute(ModelAndView modelAndView, CountDownLatch latch) throws Exception {
        try {

            //查询
            Object obj = redisUtil.get(cache_key);
            Object objMinutes = redisUtil.get(cache_key_minutes);
            Object objToDayAmount = redisUtil.get(cache_key_toDay);
            Object objyestoDayAmount = redisUtil.get(cache_key_yesToday);
            TodayQinXuDto todayQinXu;
            if (null == obj) {
                todayQinXu = QueryBanKuaiAndDaPan.getTodayQinXu();
                List<String> days = todayQinXuMapper.queryDates();
                List<TodayQinXuDto> todayQinXuDtos = todayQinXuMapper.queryTodayAndYesTodayAmount(days.get(0), days.get(1));

                //分钟
                List<String> minutes = todayQinXuDtos.stream().map(TodayQinXuDto::getMinutes).collect(Collectors.toList());
                redisUtil.set(TodayQinXuTask.cache_key_minutes, JSONObject.toJSONString(minutes));

                //今日
                List<BigDecimal> todayAmounts = todayQinXuDtos.stream().map(TodayQinXuDto::getTodayTradeAmount).collect(Collectors.toList());
                redisUtil.set(TodayQinXuTask.cache_key_toDay, JSONObject.toJSONString(todayAmounts));

                //昨日
                List<BigDecimal> yesTodayAmounts = todayQinXuDtos.stream().map(TodayQinXuDto::getYesTodayTradeAmount).collect(Collectors.toList());
                redisUtil.set(TodayQinXuTask.cache_key_yesToday, JSONObject.toJSONString(yesTodayAmounts));
            } else {
                todayQinXu = JSONObject.parseObject(obj.toString(), TodayQinXuDto.class);
                List<String> objMinutes_ = JSONObject.parseArray(objMinutes.toString(),String.class);
                modelAndView.addObject("objMinutes_", objMinutes_);
                List<BigDecimal> objToDayAmount_ =JSONObject.parseArray(objToDayAmount.toString(),BigDecimal.class);
                modelAndView.addObject("objToDayAmount_", objToDayAmount_);
                List<BigDecimal> objyestoDayAmount_ = JSONObject.parseArray(objyestoDayAmount.toString(),BigDecimal.class);
                modelAndView.addObject("objyestoDayAmount_", objyestoDayAmount_);

            }

            modelAndView.addObject("todayQinXu", todayQinXu);

            log.info("今日大盘 = ", JSONObject.toJSONString(todayQinXu));
            //
            setTodayQinXuTask(JSONObject.toJSONString(todayQinXu));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(null!=latch){
                log.info("今日大盘 执行完成，当前剩余条数:{}", latch.getCount());
                latch.countDown();
            }
        }
    }
}
